package com.dialogic.conference;

import java.util.ArrayList;

import com.dialogic.conference.Model.Configuration;
import com.dialogic.conference.Model.Configuration.CONFIGURATION_ENTRY;
import com.dialogic.conference.Model.Configuration.CONFIGURATION_SECTION;
import com.dialogic.conference.Screens.Screen;
import com.dialogic.conference.Screens.ScreenNetwork;
import com.dialogic.conference.Screens.Screen.SCREEN_TYPE;
import com.dialogic.conference.Services.IConfigurationService;
import com.dialogic.conference.Services.Impl.ServiceManager;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class SimpleConference extends Activity
{
	SharedPreferences mConferenceSettings;
	public static final String CONFERENCE_PREFERENCES = "Preferences";
	public static final String CONFERENCE_PREFERENCES_CONFERENCE_ID = "ConferenceId";
	public static final String CONFERENCE_PREFERENCES_CONFERENCE_NAME = "ConferenceName";
	public static final String CONFERENCE_PREFERENCES_IP = "ConferenceIp";
	public static String ConferenceName;
	
	public static String getConferenceName() {
		return ConferenceName;
	}

	private Spinner spinner;
	private ArrayAdapter<String> adapter;
	private ArrayList<String> channels = new ArrayList<String>();
	
	private int selectedChannel;
	private String selectedChannelName;
	private EditText ipAddress;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.simple_conference);
        
        channels.add("Dialogic Sales Kick Off");
        channels.add("Dialogic University");
        channels.add("IT Assistance");
        channels.add("Singles Party Line");
        channels.add("Email2Fax Fan Club");
        channels.add("Cutting Edge T.30 Fax");
        channels.add("IPMS On The Move");
        channels.add("The Conferencing Conference Line");
        channels.add("Waterproofing IPMS");
        //adapter.notifyDataSetChanged();
        
        // Retrieve the shared preferences
        mConferenceSettings = getSharedPreferences(CONFERENCE_PREFERENCES, Context.MODE_PRIVATE);
        
        
        spinner = (Spinner) findViewById(R.id.simple_conference_channel_spinner);
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, channels);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(adapter);

        spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
        	
		@Override
         public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long id) {               
                selectedChannel 	= position;
                selectedChannelName = channels.get(position);
         }

         @Override
         public void onNothingSelected(AdapterView<?> arg0) {
         }
        });
        
        selectedChannel = mConferenceSettings.getInt(CONFERENCE_PREFERENCES_CONFERENCE_ID, 0);
        spinner.setSelection(selectedChannel);
        ConferenceName = new String(channels.get(selectedChannel));
        
        Button button = (Button) findViewById(R.id.simple_conference_button);
        button.setOnClickListener( new OnClickListener() {
			public void onClick(View v) {
				updateStorage();
				jumpToMainScreen();
			}
		});
        
        ipAddress = (EditText) findViewById(R.id.simple_conference_ip_edit_text);
		ipAddress.setText(mConferenceSettings.getString(CONFERENCE_PREFERENCES_IP, "<fill in IP Address>"));
    }
    
    private void updateStorage()
    {
    	ConferenceName = new String(channels.get(selectedChannel));
    	Editor editor = mConferenceSettings.edit();
    	editor.putInt(CONFERENCE_PREFERENCES_CONFERENCE_ID, selectedChannel);
        editor.putString(CONFERENCE_PREFERENCES_CONFERENCE_NAME, selectedChannelName);
        editor.putString(CONFERENCE_PREFERENCES_IP, ((EditText) findViewById(R.id.simple_conference_ip_edit_text)).getText().toString());
        editor.commit();
    }
    
    private void jumpToMainScreen()
    {
    	try {
			startActivity(new Intent(this, Main.class));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.exit(0);
    }
    
    @Override
    public void onPause()
    {
    	super.onPause();
    	updateStorage();
    }
}
